﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.Modal;
using Core.Core;
using System.Text;

namespace www.pages.project
{
    public partial class project : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                hidProject_id.Value = Request["id"] ?? "0";
                BindData(hidProject_id.Value);
            }
        }

        private void BindData(string id)
        {
            modal_Project project = ProjectUtil.Get(Convert.ToInt32(id));
            if (project != null) {
                lbPrject.Text = project.project_name;
                lbComment.Text = project.project_comment;
                lbDate.Text = Convert.ToDateTime(project.create_date).ToString("yyyy/MM/dd HH:mm");
            }

            BindDocuments();
            BindTags();
        }

        #region documents


        protected void btnSearchDocs_Click(object sender, EventArgs e)
        {
            BindDocuments();
        }

        private string InitQueryDocs() {
            StringBuilder sbQuery = new StringBuilder($"project_id='{hidProject_id.Value}'");
            if (txtDocName.Value != "") {
                sbQuery.Append($" and doc_name like '%{txtDocName.Value.Trim()}%'");
            }
            return sbQuery.ToString();
        }

        private void BindDocuments() {
            int recordcount = 0;

            this.list_data.DataSource = DocumentUtil.GetList(InitQueryDocs(), "create_date desc", PagerDoc.CurrentPageIndex, PagerDoc.PageSize, ref recordcount);
            this.list_data.DataBind();

            PagerDoc.RecordCount = recordcount;
        }

        protected void PagerDoc_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            PagerDoc.CurrentPageIndex = e.NewPageIndex;
            BindDocuments();
        }
        #endregion

        #region tags
        protected void btnSearchTag_Click(object sender, EventArgs e)
        {
            BindTags();
        }

        private string InitQueryTags() {
            StringBuilder sbQuery = new StringBuilder($"project_id='{hidProject_id.Value}'");
            if (txtTag.Value != "")
            {
                sbQuery.Append($" and tag_name like '%{txtTag.Value.Trim()}%'");
            }
            return sbQuery.ToString();
        }
        private void BindTags() {
            int recordcount = 0;

            this.list_tags.DataSource = TagUtil.GetList(InitQueryTags(), "create_date desc",PagerTag.CurrentPageIndex,PagerTag.PageSize,ref recordcount);
            this.list_tags.DataBind();

            PagerTag.RecordCount = recordcount;
        }

        protected void PagerTag_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            PagerTag.CurrentPageIndex = e.NewPageIndex;
            BindTags();
        }
        #endregion

        #region 前台方法
        public string ShowHtmlDocs(string type,string tag_id,int size) {
            if (type == "exclusive") {
                return Core.Helper.StringUtil.subString(Core.Core.TagUtil.TagFitDocuments(Convert.ToInt32(hidProject_id.Value), Convert.ToInt32(tag_id)), size, true);
            }
            else {
                return "所有文档适用";
            }
        }
        #endregion
    }
}